﻿/*++

Copyright (c) 2011 Rainr Corporation

Module Name:

	Rainr.Utilities.Common.TypeConvert

Abstract:

	Type Converter 

Author:

	Zhou Junyu (Rainr)    2011-01-01

Revision History:
  
--*/

using System;
using System.Text;
using System.Text.RegularExpressions;

namespace Rainr.Utilities.Common
{
    /// <summary>
    /// 类型转换类
    /// </summary>
    public static class TypeConvert
    {
        /// <summary>
        /// String型转换为Bool型
        /// </summary>
        /// <param name="stringValue">要转换的字符串</param>
        /// <param name="defaultValue">转换失败返回的缺省值</param>
        /// <returns>转换后的bool类型结果</returns>
        public static bool ToBool(string stringValue, bool defaultValue)
        {
            if (!string.IsNullOrEmpty(stringValue))
            {
                if (0 == string.Compare(stringValue, "true", true))
                    return true;
                else if (0 == string.Compare(stringValue, "false", true))
                    return false;
            }
            return defaultValue;
        }
        public static bool ToBool(string stringValue)
        {
            return ToBool(stringValue, false);
        }
        public static bool ToBool(object objectValue, bool defaultValue)
        {
            if (objectValue != null)
            {
                return ToBool(objectValue.ToString(), defaultValue);
            }
            return defaultValue;
        }
        public static bool ToBool(object objectValue)
        {
            return ToBool(objectValue, false);
        }

        /// <summary>
        /// string型转换为int型
        /// </summary>
        /// <param name="stringValue">string型值</param>
        /// <param name="defaultValue">转换失败返回的默认值</param>
        /// <returns></returns>
        public static int ToInt32(string stringValue, int defaultValue)
        {
            if (!string.IsNullOrEmpty(stringValue))
            {
                int rv = 0;
                if (int.TryParse(stringValue, out rv))
                    return rv;
                else
                    return Convert.ToInt32(ToFloat(stringValue, defaultValue));
            }
            return defaultValue;
        }
        public static int ToInt32(object objectValue, int defaultValue)
        {
            if (objectValue != null)
                return ToInt32(objectValue.ToString(), defaultValue);
            return defaultValue;
        }
        public static int ToInt32(string stringValue)
        {
            return ToInt32(stringValue, 0);
        }
        public static int ToInt32(object objectValue)
        {
            return ToInt32(objectValue, 0);
        }

        public static long ToInt64(string stringValue, long defaultValue)
        {
            if (!string.IsNullOrEmpty(stringValue))
            {
                long rv = 0;
                if (long.TryParse(stringValue, out rv))
                    return rv;
                else
                    return Convert.ToInt64(ToDouble(stringValue, defaultValue));
            }
            return defaultValue;
        }
        public static long ToInt64(object objectValue, long defaultValue)
        {
            if (objectValue != null)
                return ToInt64(objectValue.ToString(), defaultValue);
            return defaultValue;
        }
        public static long ToInt64(string stringValue)
        {
            return ToInt64(stringValue, 0);
        }
        public static long ToInt64(object objectValue)
        {
            return ToInt64(objectValue, 0);
        }

        /// <summary>
        /// string型转换成float型
        /// </summary>
        /// <param name="stringValue">欲转换的string值</param>
        /// <param name="defaultValue">转换失败返回的默认值</param>
        /// <returns></returns>
        public static float ToFloat(string stringValue, float defaultValue)
        {
            if (!string.IsNullOrEmpty(stringValue))
            {
                float rv = 0;
                if (float.TryParse(stringValue, out rv))
                    return rv;
            }
            return defaultValue;
        }
        public static float ToFloat(object objectValue, float defaultValue)
        {
            if (objectValue != null)
            {
                return ToFloat(objectValue.ToString(), defaultValue);
            }
            return defaultValue;
        }
        public static float ToFloat(string stringValue)
        {
            return ToFloat(stringValue, 0);
        }
        public static float ToFloat(object objectValue)
        {
            return ToFloat(objectValue, 0);
        }

        public static double ToDouble(string stringValue, double defaultValue)
        {
            if (!string.IsNullOrEmpty(stringValue))
            {
                double rv = 0;
                if (double.TryParse(stringValue, out rv))
                    return rv;
            }
            return defaultValue;
        }
        public static double ToDouble(object objectValue, double defaultValue)
        {
            if (objectValue != null)
            {
                return ToDouble(objectValue.ToString(), defaultValue);
            }
            return defaultValue;
        }
        public static double ToDouble(string stringValue)
        {
            return ToDouble(stringValue, 0);
        }
        public static double ToDouble(object objectValue)
        {
            return ToDouble(objectValue, 0);
        }

        /// <summary>
        /// string型转换为datetime型
        /// </summary>
        /// <param name="stringValue">欲转换的string值</param>
        /// <param name="defaultValue">转换失败返回的默认值</param>
        /// <returns></returns>
        public static DateTime ToDateTime(string stringValue, DateTime defaultValue)
        {
            if (!string.IsNullOrEmpty(stringValue))
            {
                DateTime dt;
                if (DateTime.TryParse(stringValue, out dt))
                    return dt;
            }
            return defaultValue;
        }
        public static DateTime ToDateTime(object objectValue, DateTime defaultValue)
        {
            if (objectValue != null)
            {
                DateTime dt;
                if (DateTime.TryParse(objectValue.ToString(), out dt))
                    return dt;
            }
            return defaultValue;
        }
        public static DateTime ToDateTime(string stringValue)
        {
            return ToDateTime(stringValue, DateTime.Now);
        }
        public static DateTime ToDateTime(object objectValue)
        {
            return ToDateTime(objectValue, DateTime.Now);
        }

        /// <summary>
        /// 将字符串转换为字节流
        /// </summary>
        /// <param name="stringValue">字符串值</param>
        /// <param name="defaultEncoding">默认的编码类型</param>
        /// <returns>转换后的字节流</returns>
        public static byte[] ToBytes(string stringValue, Encoding defaultEncoding)
        {
            return defaultEncoding.GetBytes(stringValue);
        }
        public static byte[] ToBytes(string stringValue)
        {
            return ToBytes(stringValue, Encoding.UTF8);
        }
        public static byte[] ToBytes(char charValue)
        {
            return BitConverter.GetBytes(charValue);
        }
        public static byte[] ToBytes(uint uintValue)
        {
            return BitConverter.GetBytes(uintValue);
        }
        public static byte[] ToBytes(ulong ulongValue)
        {
            return BitConverter.GetBytes(ulongValue);
        }
        public static byte[] ToBytes(int intValue)
        {
            return BitConverter.GetBytes(intValue);
        }
        public static byte[] ToBytes(long longValue)
        {
            return BitConverter.GetBytes(longValue);
        }
        public static byte[] ToBytes(float floatValue)
        {
            return BitConverter.GetBytes(floatValue);
        }
        public static byte[] ToBytes(double doubleValue)
        {
            return BitConverter.GetBytes(doubleValue);
        }

        /// <summary>
        /// 字符串转为换Base64字符
        /// </summary>
        /// <param name="stringValue">输入字符串</param>
        /// <param name="defaultEncoding">默认编码</param>
        /// <returns>Base64字符串</returns>
        public static string ToBase64String(string stringValue, Encoding defaultEncoding)
        {
            return Convert.ToBase64String(ToBytes(stringValue, defaultEncoding));
        }
        public static string ToBase64String(string stringValue)
        {
            return Convert.ToBase64String(ToBytes(stringValue, Encoding.UTF8));
        }
        public static string ToBase64String(byte[] bytesValue)
        {
            return Convert.ToBase64String(bytesValue);
        }

    }
}
